package com.fs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.mapper.OrderDetailMapper;
import com.fs.pojo.OrderDetail;
import com.fs.service.OrderDetailService;
import com.fs.vo.Condition;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yyds
 * @fileName OrderDetailServiceImpl
 * @date 2024/10/17 10:21
 * @description 订单（进货退货）商品详细服务方法
 **/
@Service
public class OrderDetailServiceImpl implements OrderDetailService {
    @Resource
    OrderDetailMapper orderDetailMapper;

    /**
     * 获取当前订单编号的所有商品详情
     * @param condition
     * @return
     */
    @Override
    public PageInfo<OrderDetail> getOrderDetailList(Condition<OrderDetail> condition) {
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        if (condition.getSearch() != null){
            if (condition.getSearch().getStoProId() != null&& condition.getSearch().getStoProId() != ' ' ){
                wrapper.eq("sto_pro_id",condition.getSearch().getStoProId());
            }
            if (condition.getSearch().getProductId() != null && condition.getSearch().getProductId() != ' '){
                wrapper.eq("product_id",condition.getSearch().getProductId());
            }
            if (condition.getSearch().getProductName() != null && !condition.getSearch().getProductName().trim().isEmpty()){
                wrapper.eq("product_name",condition.getSearch().getProductName());
            }
        }
        PageHelper.startPage(condition.getPage(), condition.getCount());
        List<OrderDetail> list = orderDetailMapper.selectList(wrapper);
        return new PageInfo<>(list);
    }

    /**
     * 给当前订单编号添加商品详情
     * @param orderDetail
     * @return
     */
    @Override
    public Integer addOrderDetail(OrderDetail orderDetail) {
        System.out.println(orderDetail.toString());
        return orderDetailMapper.insert(orderDetail);
    }

    /**
     * 修改当前商品编号的详情单
     * @param orderDetail
     * @return
     */
    @Override
    public Integer updateOrderDetail(OrderDetail orderDetail) {
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id",orderDetail.getProductId()).eq("sto_pro_id",orderDetail.getStoProId());
        return orderDetailMapper.update(orderDetail,wrapper);
    }

    /**
     * 删除当前商品编号的商品
     * @return
     */
    @Override
    public Integer delOrderDetailById(OrderDetail orderDetail) {
        System.out.println("测试"+orderDetail);
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id",orderDetail.getProductId()).eq("sto_pro_id",orderDetail.getStoProId());
        return orderDetailMapper.delete(wrapper);
    }
}
